import Foundation
import CoreLocation

struct GeocodingTestConfig {
    // MARK: - 海外测试坐标
    static let overseasTestCoordinates: [OverseasTestCoordinate] = [
        OverseasTestCoordinate(
            name: "日本 - 东京 (市中心区域)",
            country: "日本",
            location: "东京",
            coordinate: CLLocationCoordinate2D(latitude: 35.6895, longitude: 139.6917),
            description: "东京市中心区域，日本首都"
        ),
        OverseasTestCoordinate(
            name: "美国 - 纽约 (时代广场)",
            country: "美国", 
            location: "纽约",
            coordinate: CLLocationCoordinate2D(latitude: 40.7580, longitude: -73.9855),
            description: "纽约时代广场，美国著名地标"
        ),
        OverseasTestCoordinate(
            name: "泰国 - 曼谷 (大皇宫附近)",
            country: "泰国",
            location: "曼谷", 
            coordinate: CLLocationCoordinate2D(latitude: 13.7500, longitude: 100.4925),
            description: "曼谷大皇宫附近，泰国著名景点"
        ),
        OverseasTestCoordinate(
            name: "英国 - 伦敦 (特拉法加广场)",
            country: "英国",
            location: "伦敦",
            coordinate: CLLocationCoordinate2D(latitude: 51.5074, longitude: -0.1278),
            description: "伦敦特拉法加广场，英国著名地标"
        )
    ]
    
    // MARK: - 测试结果记录
    static func saveTestResult(_ result: OverseasTestResult) {
        var results = loadTestResults()
        results.append(result)
        
        // 只保留最近50条记录
        if results.count > 50 {
            results = Array(results.suffix(50))
        }
        
        if let data = try? JSONEncoder().encode(results) {
            UserDefaults.standard.set(data, forKey: "OverseasGeocodingTestResults")
            print("💾 海外地理编码测试结果已保存: \(results.count) 条记录")
        }
    }
    
    static func loadTestResults() -> [OverseasTestResult] {
        guard let data = UserDefaults.standard.data(forKey: "OverseasGeocodingTestResults"),
              let results = try? JSONDecoder().decode([OverseasTestResult].self, from: data) else {
            return []
        }
        return results
    }
    
    static func clearTestResults() {
        UserDefaults.standard.removeObject(forKey: "OverseasGeocodingTestResults")
        print("🗑️ 海外地理编码测试结果已清除")
    }
}

// MARK: - 海外测试坐标模型
struct OverseasTestCoordinate: Codable {
    let name: String
    let country: String
    let location: String
    let coordinate: CLLocationCoordinate2D
    let description: String
    
    enum CodingKeys: String, CodingKey {
        case name, country, location, description
        case latitude, longitude
    }
    
    init(name: String, country: String, location: String, coordinate: CLLocationCoordinate2D, description: String) {
        self.name = name
        self.country = country
        self.location = location
        self.coordinate = coordinate
        self.description = description
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        country = try container.decode(String.self, forKey: .country)
        location = try container.decode(String.self, forKey: .location)
        description = try container.decode(String.self, forKey: .description)
        
        let latitude = try container.decode(Double.self, forKey: .latitude)
        let longitude = try container.decode(Double.self, forKey: .longitude)
        coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        try container.encode(country, forKey: .country)
        try container.encode(location, forKey: .location)
        try container.encode(description, forKey: .description)
        try container.encode(coordinate.latitude, forKey: .latitude)
        try container.encode(coordinate.longitude, forKey: .longitude)
    }
}

// MARK: - 海外测试结果模型
struct OverseasTestResult: Codable {
    let testTime: Date
    let coordinate: OverseasTestCoordinate
    let success: Bool
    let responseTime: TimeInterval
    let httpStatusCode: Int?
    let resultAddress: String?
    let errorMessage: String?
    let rawResponse: String?
    
    var formattedTestTime: String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter.string(from: testTime)
    }
    
    var formattedResponseTime: String {
        return String(format: "%.3f秒", responseTime)
    }
} 